//
//  VisitorView.swift
//  WeiboClassSix
//
//  Created by HM on 16/10/6.
//  Copyright © 2016年 HM. All rights reserved.
//

import UIKit

class VisitorView: UIView {

    //大图标
    lazy var iconImageView: UIImageView = UIImageView(imageName:"visitordiscover_feed_image_house")
    
    //菊花
    lazy var cirelimageView: UIImageView = UIImageView(imageName: "visitordiscover_feed_image_smallicon")
    
    //遮罩视图
    lazy var maskimageView: UIImageView = UIImageView(imageName: "visitordiscover_feed_mask_smallicon")
    
    //文字label
     lazy var textLabel: UILabel = UILabel(text: "欢迎来到微博首页,请先登录", fontSize: 14, alignment: .center)
    
   //注册按钮
    lazy var registButton: UIButton = UIButton(title: "注册", titleColor: globalColor, fontSize: 13,  backgroundImage: "common_button_white_disable", target: self, action: #selector(regist(button:)))
    
    //登录按钮 
    lazy var loginButton: UIButton = UIButton(title: "登录", fontSize: 13,  backgroundImage: "common_button_white_disable", target: self, action: #selector(login(button:)))
    
    init() {
        super.init(frame:screenBounds)
        
        //ui搭建
        setupUI()
    }
    
    required init?(coder aDecoder:NSCoder){
        fatalError("init(coder:) has not been implemented")
    }
    
    
}


extension VisitorView {
    func setupUI() {
        //自动布局的原则: 从上到下,从左到右,依次布局,如果控件较多,每次布局完成一个,就跑一个看看效果
        //1.添加子视图
        addSubview(iconImageView)
        addSubview(cirelimageView)
        addSubview(maskimageView)
        addSubview(textLabel)
        addSubview(registButton)
        addSubview(loginButton)
        
        //2.设置子视图
        backgroundColor = UIColor(white: 237/255.0, alpha: 1.0)
        
        //3.自动布局
        iconImageView.snp.makeConstraints {
            (make) in
            make.centerX.equalTo(self)
            make.centerY.equalTo(self).offset(-120)
        }
        
        cirelimageView.snp.makeConstraints {
            (make) in
            make.center.equalTo(iconImageView)
        }
        
        maskimageView.snp.makeConstraints {
            (make) in
            make.centerX.equalTo(self)
            make.centerY.equalTo(cirelimageView).offset(40)
            make.left.equalTo(self)
            make.right.equalTo(self)
        }
        
        textLabel.snp.makeConstraints { (make) in
            make.centerX.equalTo(self)
            make.centerY.equalTo(cirelimageView).offset(180)
            make.left.equalTo(self).offset(50)
            make.right.equalTo(self).offset(-50)
        }
        
        registButton.snp.makeConstraints { (make) in
            make.centerY.equalTo(cirelimageView).offset(280)
            make.left.equalTo(self).offset(80)
            make.size.equalTo(CGSize(width: 90, height: 35))
        }
        
        loginButton.snp.makeConstraints { (make) in
            make.centerY.equalTo(cirelimageView).offset(280)
            make.right.equalTo(self).offset(-80)
            make.size.equalTo(CGSize(width: 90, height: 35))
        }
        
        //添加动画
        addAnimation()
        
        
    }
    
    func addAnimation () {
        let amimation = CABasicAnimation(keyPath: "transform.rotation")
        amimation.duration = 10
        amimation.toValue = 2 * M_PI
        amimation.repeatCount = MAXFLOAT
        
        //当切换界面时,动画不会自动停止
        amimation.isRemovedOnCompletion = false
        
        cirelimageView.layer.add(amimation, forKey: nil)
    }
    //设置杭客视图
    func setVisitorView (title:String,icon : String, isAnimation: Bool = false){
        textLabel.text = title
        iconImageView.image = UIImage(named: icon)
        
        //3.0之后变成isHidden
        cirelimageView.isHidden = !isAnimation
        maskimageView.isHidden = !isAnimation
        
    }
}

extension VisitorView {
    @objc fileprivate func login(button:UIButton) {
        print("登陆")
    }
    
    @objc fileprivate func regist(button:UIButton) {
        print("注册")
    }
}




